package demo1;

import java.util.Comparator;

/**
 * Version: 1.1
 * Author: TangJun
 * Date: 2024-07-18
 */
public class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        //return this.name.compareTo(o.name);
        return this.age - o.age;
    }

    public static void bubleSort(Student[] stus) {
        NameComparator cmp = new NameComparator();
        for (int i = 0; i < stus.length - 1; i++) {
            for (int j = 0; j < stus.length - 1 - i; j++) {
                if (cmp.compare(stus[j], stus[j+1]) > 0) {
                    Student tmp = stus[j];
                    stus[j] = stus[j+1];
                    stus[j+1] = tmp;
                }
            }
        }
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

}
class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}

class NameComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
